package xy.http.config;

import okhttp3.OkHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import xy.http.util.OkHttpUtil;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Configuration
public class OkHttlClientConfig {

    @Resource
    OkHttpConfig okHttpConfig;

    @Bean
    @Primary
    public OkHttpUtil okHttpUtil(OkHttpClient client) {
        return new OkHttpUtil(client);
    }

    @Bean
    public OkHttpClient okHttpClient() {
        return new OkHttpClient().newBuilder()
                // 设置超时时间
                .connectTimeout(okHttpConfig.getConnectTimeOut(), TimeUnit.SECONDS)
                // 设置读取超时时间
                .readTimeout(okHttpConfig.getReadTimeOut(), TimeUnit.SECONDS)
                // 设置写入超时时间
                .writeTimeout(okHttpConfig.getWriteTimeOut(), TimeUnit.SECONDS).build();
    }
}
